//
//  AppDelegate.swift
//  Weibo
//
//  Created by 杨军民 on 16/7/18.
//  Copyright © 2016年 杨军民. All rights reserved.
//

import UIKit

/*
1、判断是否是最新版本
a、获取当前版本
b、获取最新版本
c、比较（当前版本和最新版本是否一致）
d、判断是否是最新版本
e、返回

2、判断用户是否登录
//在判断前先在YJMUserAccountViewModel显示是否登录状态
a、没有登录就去YJMMainTableViewController
b、登录就判断版本是否最新，然后就跳转

3、提供一个类方法给外面的人调用

*/

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        //创建窗口
        window = UIWindow (frame:UIScreen.mainScreen().bounds)
        
        let YJMMain = YJMMainTableViewController()
        YJMMain.view.backgroundColor = UIColor .whiteColor()
        
        //设置根跟控制器
//        window?.rootViewController = YJMMain
        window?.rootViewController = defaultViewController()
        
        //窗口显示
        window?.makeKeyAndVisible()
        
        //调用皮肤
        setUpAppearance()
        
        return true
    }
    
    //供外界调用切换界面，
    class func switchRootViewController(controller: UIViewController) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = controller
        
    }
    
    //判断User是否登录
    private func defaultViewController() ->UIViewController {
        if !YJMUserAccountViewModel.sharedUserAccountViewModel.userLogin {
            //没有登录
            return YJMMainTableViewController()
        }else{
            //登录了
            let newVersion = isNewVersion()
            return newVersion ? YJMNewFeatCollectionViewController() : YJMNweCharacteristicsController()
        }
    }
    
    //判断是否是新版本
    private func isNewVersion() ->Bool {
        
        //当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary! ["CFBundleShortVersionString"] as! String
        
        //最新版本
        let key = "lastVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(key)
        
        //比较
        let isNewVersion = currentVersion != lastVersion
        
        //判断
        if isNewVersion {
            //保存新版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        //返回
        return isNewVersion
    }
    
    //皮肤
    func setUpAppearance() {
        //创建皮肤
        let bar = UINavigationBar.appearance()
        //皮肤颜色
        bar.tintColor = UIColor .orangeColor()
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

